//
//  AppTVCaseDetailController.m
//  classicFilms
//
//  Created by 刘继丹 on 17/4/25.
//  Copyright © 2017年 Stefan Arn. All rights reserved.
//

#import "AppTVCaseDetailController.h"
#import "AppTvCaseDetailView.h"
#import "AppTVTopLoopView.h"
#import "AppTVBigImageBrowseController.h"
#import "CaseDetailApi.h"
#import "ARNTextViewController.h"

#define kBottomViewHeight 330

@interface AppTVCaseDetailController ()

@property(nonatomic, strong) NSMutableArray *mImageArr;

@property(nonatomic, strong) AppTvCaseDetailView *mAppTvCaseDetailView;

@property(nonatomic, strong) UIView *mBottomView;
@property(nonatomic, strong) UILabel *mTitleLabel;
@property(nonatomic, strong) AppTVTopLoopView *mAppTVTopLoopView;
@property(nonatomic, strong) NSDictionary *mDataDic;

@end

@implementation AppTVCaseDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    _mImageArr = [NSMutableArray array];
//    for (int i = 0; i < 15; i++) {
//        [_mImageArr addObject:@"http://shop-static.halobear.com/wedding/Fmy_D7GPBYkBTSobZydqE1hMmqTU.jpg-w750n"];
//    }
    
    [self.view addSubview:self.mAppTvCaseDetailView];
    [self.view addSubview:self.mBottomView];
    [self.mBottomView addSubview:self.mTitleLabel];
    [self.mBottomView addSubview:self.mAppTVTopLoopView];
    
    [self loadNewData];
    
}

-(void)loadNewData{
    
    CaseDetailApi *caseDetailApi = [[CaseDetailApi alloc]initWithCase_id:self.caseId];
    
    [caseDetailApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSLog(@"======%@", request.responseJSONObject);
        _mDataDic = [[request.responseJSONObject objectForKey:@"data"] objectForKey:@"demo"];
        _mImageArr = [[request.responseJSONObject objectForKey:@"data"] objectForKey:@"attachs"];
        _mAppTvCaseDetailView.dic = _mDataDic;
        _mAppTVTopLoopView.imageArr = _mImageArr;

    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
    
}

#pragma mark- configView
-(AppTvCaseDetailView *)mAppTvCaseDetailView{
    if (!_mAppTvCaseDetailView) {
        _mAppTvCaseDetailView = [[AppTvCaseDetailView alloc]initWithFrame:CGRectMake(0, 0, KVIEW_W, KVIEW_H-kBottomViewHeight)];
        
        __weak typeof(self) weakSelf = self;
        
        _mAppTvCaseDetailView.detailLabelTouchBlock = ^(NSString *content){
            ARNTextViewController *textViewController = [ARNTextViewController new];
            textViewController.label.text = content;
            textViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [weakSelf presentViewController:textViewController animated:YES completion:nil];
        };
        
    }
    return _mAppTvCaseDetailView;
}

-(UIView *)mBottomView{
    if (!_mBottomView) {
        _mBottomView = [[UIView alloc]initWithFrame:CGRectMake(0, KVIEW_H-kBottomViewHeight, KVIEW_W, kBottomViewHeight)];
        _mBottomView.backgroundColor = kDetailBottomBackGroundColor_black;
        [_mBottomView addLineUp:YES andDown:NO];
    }
    return _mBottomView;
}

-(UILabel *)mTitleLabel{
    if (!_mTitleLabel) {
        _mTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 0, KVIEW_W-90, 70)];
        _mTitleLabel.text = @"案例详情";
        _mTitleLabel.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
        _mTitleLabel.font = [UIFont systemFontOfSize:20];
    }
    return _mTitleLabel;
}

-(AppTVTopLoopView *)mAppTVTopLoopView{
    if (!_mAppTVTopLoopView) {
        _mAppTVTopLoopView = [[AppTVTopLoopView alloc]initWithFrame:CGRectMake(0, 60, KVIEW_W, kBottomViewHeight-120) type:AppTVLoopViewType_CaseDetail];
        
        __weak typeof(self) weakSelf = self;
        //图片选择
        _mAppTVTopLoopView.imageSelectBlock = ^(NSInteger selectIndex){
            
            AppTVBigImageBrowseController *imageBrowse = [[AppTVBigImageBrowseController alloc]init];
            
            NSMutableArray *dataArr = [[NSMutableArray alloc] init];
            for (int i = 0; i<weakSelf.mImageArr.count; i++) {
                NSDictionary *dic = [weakSelf.mImageArr objectAtIndex:i];
                NSString *name = [dic valueForKey:@"src_url"];
                [dataArr addObject:name];
            }
            imageBrowse.scrollIndex = selectIndex;
            imageBrowse.imageArr = dataArr;
            [weakSelf presentViewController:imageBrowse animated:YES completion:^{}];
        };
    }
    return _mAppTVTopLoopView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
